//
//  XJRecommendGoodsViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/11.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJRecommendGoodsViewController.h"
#import "XJGoodsDetailViewController.h"

#import "XJRecommendGoodsCollectionViewCell.h"

#import "XJGoodsOverFooter.h"
// model
#import "RecommendGoodsModel.h"


@interface XJRecommendGoodsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

/* collectionView */
@property (nonatomic, strong) UICollectionView *collectionView;

/**数据源*/
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

static NSString * const RecommendCellID = @"XJRecommendGoodsCollectionViewCell";
static NSString *const OverFooterID = @"XJGoodsOverFooter";


@implementation XJRecommendGoodsViewController

#pragma mark 懒加载
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        //注册
        //cell
        [_collectionView registerClass:[XJRecommendGoodsCollectionViewCell class] forCellWithReuseIdentifier:RecommendCellID];
        //footer
        [_collectionView registerClass:[XJGoodsOverFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:OverFooterID];
        
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}


- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"推荐商品";
//self.navButtonLeft.hidden = NO;
  //  self.navButtonRight.hidden = YES;
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.frame = CGRectMake(0, 0, KWidth, KHeight - XJBottomTabH);
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = WHITE_COLOR;
    
    [self requestData];
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    if (section == 0) {
//        return 9;
//    }else{
//        return 0;
//    }
    
    return self.dataArray.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XJRecommendGoodsCollectionViewCell  * xjrgCell = [collectionView dequeueReusableCellWithReuseIdentifier:RecommendCellID forIndexPath:indexPath];
    
    RecommendGoodsModel *model = self.dataArray[indexPath.row];
    
    xjrgCell.rgModel = model;
    
    return xjrgCell;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader){
        return nil;
    }else if (kind == UICollectionElementKindSectionFooter){
        if (indexPath.section==0) {
            XJGoodsOverFooter * overFooter = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:OverFooterID forIndexPath:indexPath];
            return overFooter;
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}

#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0){
        return CGSizeMake((KWidth - 4 )/2, UI_IS_IPHONE5 ? ADAPTHeight(280) : ADAPTHeight(260));
    }else{
        return CGSizeZero;
    }
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    if (indexPath.section == 4) {
        if (indexPath.row == 0) {
            layoutAttributes.size = CGSizeMake(KWidth, KWidth * 0.38);
        }else if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4){
            layoutAttributes.size = CGSizeMake(KWidth * 0.5, KWidth * 0.24);
        }else{
            layoutAttributes.size = CGSizeMake(KWidth * 0.25, KWidth * 0.35);
        }
    }
    return layoutAttributes;
}


#pragma mark - head宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeZero;
}

#pragma mark - foot宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(KWidth, ADAPTHeight(60));  //Top头条的宽高
    }else{
        return CGSizeZero;
    }
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1 || section == 2) {
        return 1;
    }else{
        return 0;
    }
}
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1 || section == 2) {
        return 1;
    }else{
        return 0;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        XJLog(@"点击了推荐的第%zd个商品",indexPath.row);
//        [XJGoodsDetailViewController initWithController:self goodsType:GoodsDetailTypeRecommend];

    }
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
   // self.tabBarController.tabBar.hidden=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 五:请求接口
- (void)requestData {
// page page_size  customer_id
    NSString *customId = [[NSUserDefaults standardUserDefaults] objectForKey:@"customer_id"];
    if ([NSString isEmptyString:customId]) {
        XJLog(@"customer_id 为空 操作无效");
        return;
    }
    
    NSDictionary *dic = @{@"customer_id":customId};
    
    
    [XJHttpTool requestRecomendGoodsWithDic:dic successH:^(id responseObjc) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObjc options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        
        XJLog(@"%@",jsonStr);
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            NSArray * recommend_listArr = responseObjc[@"data"][@"recommend_list"];
            for (NSDictionary *dic in recommend_listArr) {
                RecommendGoodsModel *model = [RecommendGoodsModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
            
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
